package com.gykfbxxh.imovie.custom.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gykfbxxh.imovie.R;


/**
 * Created by Guo on 2016/9/26.
 * <p/>
 * 带有不同EmptyView的问题；
 */
public class LoadView extends FrameLayout {

    private LinearLayout loadingPage;
    private LinearLayout errorPage;
    private LinearLayout emptyPage;
    private ImageView ivLoading;
    private ImageView civNoData;
    private ImageView ivEmpty;
    private TextView tvEmptyBloid;
    private TextView tvEmpty;

    private AnimationDrawable anim;
    private AnimationDrawable animNoNet;
    private TextView tvError;
    private TextView tvLoad;
    private FrameLayout loadViewContainer;
    private TextView tvEmptyGo;

    public LoadView(Context context) {
        this(context, null);
    }

    public LoadView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }


    /**
     * 初始化视图
     *
     * @param context
     */
    private void initView(Context context, AttributeSet attrs) {
        inflate(context, R.layout.layout_public_loadview, this);
        loadViewContainer = (FrameLayout) findViewById(R.id.loadview_container);
        loadingPage = (LinearLayout) findViewById(R.id.loading_page);
        errorPage = (LinearLayout) findViewById(R.id.error_page);
        emptyPage = (LinearLayout) findViewById(R.id.empty_page);

        //三张图片
        ivLoading = (ImageView) findViewById(R.id.iv_loading);
        civNoData = (ImageView) findViewById(R.id.civ_nodata);
        ivEmpty = (ImageView) findViewById(R.id.iv_empty);


        //正在加载的提示文字
        tvLoad = (TextView) findViewById(R.id.load);

        //emptyView的提示文字
        tvEmptyBloid = (TextView) findViewById(R.id.empty_text_bold);
        tvEmpty = (TextView) findViewById(R.id.empty_text);
        tvEmptyGo = (TextView) findViewById(R.id.empty_text_go);

        //ErrorView的提示文字
        tvError = (TextView) findViewById(R.id.error_text);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadView);
        int color = typedArray.getColor(R.styleable.LoadView_loadviewBg, getResources().getColor(R.color.app_background));
        typedArray.recycle();
        if (loadViewContainer != null) loadViewContainer.setBackgroundColor(color);


        anim = null;
        anim = (AnimationDrawable) ivLoading.getDrawable();
        ivLoading.post(new Runnable() {

            @Override
            public void run() {
                anim.start();
            }
        });
        ivLoading.setDrawingCacheBackgroundColor(0xffff0000);//设置RPG565节约内存
        setAnimationCacheEnabled(false);//禁止view的动画缓存


    }


    /**
     * 设置重新加载的事件
     *
     * @param onclick
     */
    public void setErrorPageClickListener(OnClickListener onclick) {
        if (errorPage != null) {
            this.errorPage.setOnClickListener(onclick);
        }
    }
    /**
     * 设置重新加载的事件
     *
     * @param onclick
     */
    public void setLoadingPageClickListener(OnClickListener onclick) {
        if (loadingPage != null) {
            this.loadingPage.setOnClickListener(onclick);
        }
    }

    /**
     * 设置Empty 文字的跳转
     * @param onClickListener
     */
    public void setEmptyGoClickListener(OnClickListener onClickListener){
        if(tvEmptyGo != null){
            this.tvEmptyGo.setOnClickListener(onClickListener);
        }
        if(emptyPage != null){
            this.emptyPage.setOnClickListener(onClickListener);
        }
    }






    /**
     * 展示正在加载页面
     */
    public void showLoadPage(CharSequence msg) {
        if (loadingPage == null || errorPage == null || emptyPage == null) {
            return;
        }
        if (anim != null && ivLoading != null) {
            anim = (AnimationDrawable) ivLoading.getDrawable();
            ivLoading.post(new Runnable() {

                @Override
                public void run() {
                    anim.start();
                }
            });
        }
        if (!TextUtils.isEmpty(msg) && tvLoad != null) tvLoad.setText(msg);

        loadingPage.setVisibility(View.VISIBLE);
        errorPage.setVisibility(View.GONE);
        emptyPage.setVisibility(View.GONE);

    }

    /**
     * 展示网络错误页面
     */
    public void showErrorPage() {
        if (loadingPage == null || errorPage == null || emptyPage == null) {
            return;
        }
        if (anim != null && anim.isRunning()) {
            //停止蹦跑动画
            anim.stop();
        }

//        if (civNoData != null){
//            if(animNoNet == null){
//                animNoNet = (AnimationDrawable) civNoData.getDrawable();
//            }
//            civNoData.post(new Runnable() {
//
//                @Override
//                public void run() {
//                    animNoNet.start();
//                }
//            });
//            civNoData.setDrawingCacheBackgroundColor(0xffff0000);//设置RPG565节约内存
//        }
//            LoadImageUtils.getInstance().loadImageGif(civNoData, R.drawable.no_network);

        errorPage.setVisibility(View.VISIBLE);
        loadingPage.setVisibility(View.GONE);
        emptyPage.setVisibility(View.GONE);
    }


    public void showErrorPage(CharSequence errorMsg) {
        if (loadingPage == null || errorPage == null || emptyPage == null) {
            return;
        }
        if (anim != null && anim.isRunning()) {
            //停止蹦跑动画
            anim.stop();
        }
        if (tvError != null && !TextUtils.isEmpty(errorMsg)) tvError.setText(errorMsg);
//        if (civNoData != null){
//            if(animNoNet == null){
//                animNoNet = (AnimationDrawable) civNoData.getDrawable();
//
//            }
//            civNoData.post(new Runnable() {
//
//                @Override
//                public void run() {
//                    animNoNet.start();
//                }
//            });
//            civNoData.setDrawingCacheBackgroundColor(0xffff0000);//设置RPG565节约内存
//        }

        errorPage.setVisibility(View.VISIBLE);
        loadingPage.setVisibility(View.GONE);
        emptyPage.setVisibility(View.GONE);
    }


    /**
     * 当数据为空的时候
     *
     * @param type 选择显示的空图片
     */
    public void showEmptyPage(int type, String emptyMsg) {
        if (loadingPage == null || errorPage == null || emptyPage == null
                || ivEmpty == null || tvEmpty == null || tvEmptyBloid == null) {
            return;
        }
        if (anim != null && anim.isRunning()) {
            //停止蹦跑动画
            anim.stop();
        }

        if(animNoNet != null && animNoNet.isRunning()){
            //停止挠头
            animNoNet.stop();
        }

        if (TextUtils.isEmpty(emptyMsg)) emptyMsg = getResources().getString(R.string.homepage_msg_nomsg);

        tvEmptyBloid.setVisibility(View.GONE);
        tvEmpty.setText(emptyMsg);
        ivEmpty.setImageResource(R.drawable.search_no);

        errorPage.setVisibility(View.GONE);
        loadingPage.setVisibility(View.GONE);
        emptyPage.setVisibility(View.VISIBLE);
        ivEmpty.setAlpha(1.0f);
    }

    /**
     * @param emptyMsg empty的消息提醒，暂时支持两个
     * @param resId    empty 的图片资源id
     */
    public void showEmptyPage(int resId, String... emptyMsg) {
        if (loadingPage == null || errorPage == null || emptyPage == null
                || ivEmpty == null || tvEmpty == null || tvEmptyBloid == null) {
            return;
        }
        tvEmptyBloid.setVisibility(View.GONE);
        if (anim != null && anim.isRunning()) {
            //停止蹦跑动画
            anim.stop();
        }

        if(animNoNet != null && animNoNet.isRunning()){
            //停止挠头
            animNoNet.stop();
        }

        //设置空View的提示语
        if (emptyMsg != null && emptyMsg.length > 0) {
            if (!TextUtils.isEmpty(emptyMsg[0])) tvEmpty.setText(emptyMsg[0]);
            if (emptyMsg.length > 1) {
                if (!TextUtils.isEmpty(emptyMsg[1])) {
                    tvEmptyBloid.setVisibility(View.VISIBLE);
                    tvEmptyBloid.setText(emptyMsg[1]);
                }
            }
            if(emptyMsg.length > 2){
                if(!TextUtils.isEmpty(emptyMsg[2])){
                    tvEmptyGo.setVisibility(View.VISIBLE);
                    tvEmptyGo.setText(emptyMsg[2]);
                }
            }
        }

        if (resId != 0) {
            ivEmpty.setImageResource(resId);
        } else {
            ivEmpty.setImageResource(R.drawable.search_no);
        }
        errorPage.setVisibility(View.GONE);
        loadingPage.setVisibility(View.GONE);
        emptyPage.setVisibility(View.VISIBLE);

        ivEmpty.setAlpha(1.0f);
    }


    /**
     * 加载成功
     */
    public void showSuccess() {
        if (getVisibility() != View.GONE) {
            if (anim != null && anim.isRunning()) {
                //停止蹦跑动画
                anim.stop();
            }

            if(animNoNet != null && animNoNet.isRunning()){
                //停止挠头
                animNoNet.stop();
            }
            setVisibility(View.GONE);
        }
    }


}
